String.prototype.Trim = function() {
var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/);
return (m == null) ? "" : m[1];
}
String.prototype.isMobile = function() {
return (/^(?:1\d{2})-?\d{5}(\d{3}|\*{3})$/.test(this.Trim()));
}
String.prototype.isTel = function()
{
//"兼容格式: 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"
    //return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
    return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(this.Trim())) || 
	(/^400-([0-9]){1}(-{1}[0-9]{5}|[0-9]{1}-{1}[0-9]{4}|[0-9]{2}-{1}[0-9]{3}|[0-9]{3}-{1}[0-9]{2}|[0-9]{4}-{1}[0-9]{1}|[0-9]{5}-{1})([0-9]){1}$/.test(this.Trim()));
}

var check = {
	company:{
		c:function(s){
			if(s.Trim() == '') return '公司必填';
			s = s.Trim();
			return ( !/[^\u4e00-\u9fa5]/g.test(s)  && /[\u4e00-\u9fa5]{4,}/g.test(s) ) ? 'ok' :'公司必须4位以上的中文！' ;
		},
		b:true
	},
	telephone:{
		c:function(s){
			if(s.Trim() == '') return '';
			return s.isTel() ? "ok":"电话号码不正确";
		}
	},
	mobilephone:{
		c:function(s){
			if(s.Trim() == '') return '';
			return s.isMobile() ? "ok":"手机号码不正确";
		}
	},
	qq:{
		c:function(s){
			if(s.Trim() == '') return '';
			return /[1-9][0-9]{4,}/.test(s)?'ok':'qq无效,请输入正确qq号!';
		}
	},
	email:{
		c:function(s){
			if(s.Trim() == '') return '';
			return /^[\-\.\w]+@[\.\-\w]+(\.\w+)+$/.test(s)?'ok':'Email无效,请输入有效地址!';
		}		
	}
}

$('#myform input').blur(
	function(){
		n = $(this).attr('name');
		v = $(this).val();
		c = check[n];
		if( c!=undefined ){
			d = $(this.parentNode.parentNode).find('div:last');
			if(!d.attr('b'))
			 d.attr('b',d.html());
			r = c.c(v);
			d.removeClass('right');
			d.removeClass('error');
			$(this).removeClass('error_bg');
			if(r!='' && r!='ok'){
				d.addClass('error');
				$(this).addClass('error_bg');
				d.html(r);
			}else if(r=='ok' && c.b){
				d.addClass('right');
				d.html('输入正确');
			}else if(r=='ok'){
				d.addClass('right');
				d.html('输入正确');			
			}else{
				d.html(d.attr('b'));
			}
		}
	}
);

function checkSubmit(){

	var data = {};	
	var is_error = false;
	$('#myform input,#myform select').each(
		function(i,o){
			n = $(o).attr('name');
			if( n == undefined || n=='') return;
			v = $(o).val();
			c = check[n];
			data[n] = v;
			if( c!=undefined ){
				d = $(o.parentNode.parentNode).find('div:last');
				if(!d.attr('b'))
				 d.attr('b',d.html());
				r = c.c(v);
				$(o).removeClass('error_bg');
				d.removeClass('right');
				d.removeClass('error');
				if(r!='' && r!='ok'){
					d.addClass('error');
					$(o).addClass('error_bg');
					is_error = true;
					d.html(r);
				}else if(r=='ok' && c.b){
					d.addClass('right');
					d.html('输入正确');
				}else if(r=='ok'){
					d.addClass('right');
					d.html('输入正确');			
				}else{
					d.html(d.attr('b'));
				}
			}
		}		
	);
	
	if( is_error ){
		alert('填写信息有误');
		return;
	}

	if( data['company'] == ''){
		alert('公司必填');
		return;
	}
	
	if(data['telephone']=='' && data['mobilephone']=='' && data['qq']=='' && data['email']==''){
		alert('电话，手机，QQ,Email必填一个');
		return;
	}
	$('#btnSubmit').hide();
	$.ajax({
		url:'http://www.51g3.net/g3sale/index.php?action=insert',
		data:data,
		dataType:'json',
		type:'post',
		success:function(d){
      alert(d.msg);
	  if (d.code=10){
	  location.href = 'http://www.51g3.net';
	  }else{
			$('#btnSubmit').show();
		}
		}
	});
}